package com.mchat.util;

import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.regex.Pattern;

@RequiredArgsConstructor
@Data

public class MailUtil {
    private final String sender;
     private final int validTime;
    private final JavaMailSender javaMailSender;

    private final ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(1);

    public void sendTextVerifyCode(String receiver,String code){
        executor.submit(()->{
            SimpleMailMessage mainMessage = new SimpleMailMessage();
            //发送方
            mainMessage.setFrom(sender);
            //接收方
            mainMessage.setTo(receiver);
            //发送的标题
            mainMessage.setSubject("验证码");
            //发送的内容
            mainMessage.setText("你的一次性验证码是:"+code+"!验证码有效期是"+validTime+"分钟。");
            //发送邮件
            javaMailSender.send(mainMessage);
        });
    }
    public static boolean isValid(String email){
        String reg="[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+";
        return Pattern.matches(reg, email);
    }
}
